STEP 9: Using the sonifyFormsWithHandlers Method

Up until now in this tutorial we've shown how a form can be sonified by explicitly adding event handlers to all of the elements of the form. Now, in a complex form, not only can this add quite a bit of HTML and JavaScript code to your page, but it also creates a fair amount of work for you. What if there were a wondrous and magical way to sonify a form without even touching the form's HTML? Too good to be true, right? Well, get ready to act pleasantly surprised.

At this point we'll depart into some wicked DOM (Document Object Model) Voodoo and make our lives one whole helluva lot easier. "Document Object Model?" you ask. Well, if you've ever coded an image rollover behavior for a page in a Web site, then you've already been exposed to the DOM. Basically, browsers expose all the HTML elements on a page to the JavaScript scripting environment. This is referred to as "reflection", since the HTML elements on a page are "reflected" to JavaScript in the form of objects.

Wizard to the Rescue

Now, because elements of an HTML page can be accessed using JavaScript, it becomes possible to write JavaScript code to automatically scan for specific types of elements on an HTML page and, if desired, actually modify the properties of any elements. This principle can be applied quite effectively to form elements, where it is possible using JavaScript to scan a document for all form elements and add event handlers as needed to sonify the elements.

We're not going to get into the frightening details of how this is done, because Beatnik, Inc. has already gone to the trouble of creating just the code needed to automatically sonify all elements of all forms on any HTML page. This code is neatly packaged into the Music Object Extension - Wizards module. What we will be looking at is how to use the methods provided in this extension to conveniently sonify forms in various ways. We will be continuing with the same form as we've used in the earlier steps of this tutorial, and we will show how the same results from the earlier steps can be achieved with less work using "Music Object Extension - Wizards".

In this first step using the extension, we will use Groovoids to achieve the same level of sonification as we saw in the earlier step Adding the Submit and Reset Button Sounds. And we will achieve these same results with much less sonification code, thanks to the Music.sonifyFormsWithHandlers static method found in this extension.

The Music.sonifyFormsWithHandlers method lets you sonify all the elements of all forms on any HTML page using handlers. All you need to do is supply the method with the desired handlers that are to be executed for each type of form element event that can occur. For this method, handlers must be string parameters containing the JavaScript code that is to be executed when a specific event occurs. For instance, in this page's example we have used the handler 'formSounds.play ("UI-Click7")' for when checkboxes get clicked.

Now try out the form below. Interact with it. It should sound remarkably familiar.

Blue Bass & the Neurotic Fishes
-- Merchandise --
1. Select your poison...

  T-shirt ($15.95)

Color
Size Small     Medium     Large     X-Large     XX-Large


  Commemorative Reunion Mug ($19.95) - with printed mugshot of a band member of your choice

Band Member
Mug Size


Albums ($15.95 each)

   Underwater Blues (released 1989)
   The Suffocating Sky (released 1991)
   Suckerfish Rising (released 1992)
   Psychedelic Abyss (released 1994)
   The Little Fish That Could (released 1997)
   Have You Eaten Fish Today (new release)


2. Pay for the poison...

Type of Credit Card American Express     VISA     Mastercard
*Name on Credit Card
*Credit Card Number
Expiration Date    


3. Deliver the poison...

Shipping & Handling Option
 
*Name
*Address Line 1
Address Line 2
*City
*State (or Province, or Region)
*Zip Code (or Postal Code)
*Country




There are many form element events that can be sonified automatically using the Music.sonifyFormsWithHandlers method, and a parameter is provided for each of these events, as follows...

In this example we have used almost all of the above parameters to sonify the form. Now, take a look at the HTML source code for this example to see how the Music.sonifyFormsWithHandlers method of Music Object Extension - Wizards has been used...

<HTML><HEAD><TITLE>Sonified Forms Example</TITLE></HEAD>
<BODY ONLOAD="sonifyForms ()">

<SCRIPT SRC="music-object.js"></SCRIPT>
<SCRIPT SRC="music-object-x-wizards.js"></SCRIPT>

<FORM NAME="productForm" METHOD=GET ACTION="thanks.html">
<CENTER>
<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=4>
   <TR>
      <TD ALIGN=CENTER BGCOLOR=003366>
         <FONT FACE="ARIAL,HELVETICA,VERDANA">
         <FONT COLOR=00CCFF SIZE=6>
         Blue Bass & the Neurotic Fishes<BR>
         </FONT>
         <FONT COLOR=FFFFFF SIZE=5>
         -- Merchandise --
         </FONT>
         </FONT>
      </TD>
   </TR>
   <TR>
      <TD BGCOLOR=666699>
         <FONT FACE="ARIAL,HELVETICA,VERDANA">
         <CENTER><FONT SIZE=5>1. Select your poison...</FONT></CENTER>
         <P>
         <INPUT TYPE=checkbox NAME=tshirt>  <B>T-shirt ($15.95)</B>
         <P>
         <TABLE BORDER=0 CELLSPACING=1 CELLPADDING=1>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT>Color</TD>
               <TD>
                  <SELECT NAME=tshirtColor>
                     <OPTION>Oil Slick Black
                     <OPTION>Spiritual White
                     <OPTION SELECTED>Blood Clot Red
                     <OPTION>Summer Sky Blue
                     <OPTION>Underwater Aqua
                     <OPTION>Radioactive Green
                     <OPTION>Militia Khaki
                     <OPTION>Neon Pink
                  </SELECT>
               </TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT>Size</TD>
               <TD>
                  <INPUT TYPE=radio NAME=tshirtSize>Small
                     
                  <INPUT TYPE=radio NAME=tshirtSize CHECKED>Medium
                     
                  <INPUT TYPE=radio NAME=tshirtSize>Large
                     
                  <INPUT TYPE=radio NAME=tshirtSize>X-Large
                     
                  <INPUT TYPE=radio NAME=tshirtSize>XX-Large
               </TD>
            </TR>
         </TABLE>
         <HR SIZE=1 NOSHADE>

         <INPUT TYPE=checkbox NAME=mug>  <B>Commemorative Reunion Mug ($19.95)</B>
         - with printed mugshot of a band member of your choice
         <P>
         <TABLE BORDER=0 CELLSPACING=1 CELLPADDING=1>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT>Band Member</TD>
               <TD>
                  <SELECT NAME=mugPicture>
                     <OPTION SELECTED>Blue Bass (lead vocals & bass guitar)
                     <OPTION>Alec Littlefish (lead guitar & vocals)
                     <OPTION>Joe "The Octopus" Malone (drums & percussion)
                     <OPTION>Humphrey Proudfin (keyboards & vocals)
                     <OPTION>Gill Stronglung (harmony vocals)
                     <OPTION>Neville "The Blowfish" Ohrensborg (sax & horns)
                  </SELECT>
               </TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT>Mug Size</TD>
               <TD>
                  <SELECT NAME=mugSize>
                     <OPTION>Diet Size (9 Fl.Oz.)
                     <OPTION SELECTED>Average Stomach (12 Fl.Oz)
                     <OPTION>Big Mama (20 Fl.Oz)
                  </SELECT>
               </TD>
            </TR>
         </TABLE>

         <HR SIZE=1 NOSHADE>
         <B>Albums ($15.95 each)</B>
         <P>
         <INPUT TYPE=checkbox NAME=album1989>  
            Underwater Blues (released 1989)<BR>
         <INPUT TYPE=checkbox NAME=album1991>  
            The Suffocating Sky (released 1991)<BR>
         <INPUT TYPE=checkbox NAME=album1992>  
            Suckerfish Rising (released 1992)<BR>
         <INPUT TYPE=checkbox NAME=album1994>  
            Psychedelic Abyss (released 1994)<BR>
         <INPUT TYPE=checkbox NAME=album1997>  
            The Little Fish That Could (released 1997)<BR>
         <INPUT TYPE=checkbox NAME=album1998>  
            Have You Eaten Fish Today (new release)<BR>
         <HR SIZE=1 NOSHADE>
         </FONT>
      </TD>
   </TR>
   <TR>
      <TD BGCOLOR=9999CC>
         <FONT FACE="ARIAL,HELVETICA,VERDANA">
         <CENTER><FONT SIZE=5>2. Pay for the poison...</FONT></CENTER>
         <P>
         <TABLE BORDER=0 CELLSPACING=1 CELLPADDING=1>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT>Type of Credit Card</TD>
               <TD>
                  <INPUT TYPE=radio NAME=creditCardType>American Express
                     
                  <INPUT TYPE=radio NAME=creditCardType CHECKED>VISA
                     
                  <INPUT TYPE=radio NAME=creditCardType>Mastercard
               </TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT><B>*Name on Credit Card</B></TD>
               <TD><INPUT TYPE=text SIZE=30 NAME=creditCardName></TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT><B>*Credit Card Number</B></TD>
               <TD><INPUT TYPE=text SIZE=30 NAME=creditCardNumber></TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT>Expiration Date</TD>
               <TD>
                  <SELECT NAME=creditCardExpirationYear>
                     <OPTION SELECTED>1998
                     <OPTION>1999
                     <OPTION>2000
                     <OPTION>2001
                     <OPTION>2002
                     <OPTION>2003
                     <OPTION>2004
                  </SELECT>
                     
                  <SELECT NAME=creditCardExpirationMonth>
                     <OPTION SELECTED>01 Jan
                     <OPTION>02 Feb
                     <OPTION>03 Mar
                     <OPTION>04 Apr
                     <OPTION>05 May
                     <OPTION>06 Jun
                     <OPTION>07 Jul
                     <OPTION>08 Aug
                     <OPTION>09 Sep
                     <OPTION>10 Oct
                     <OPTION>11 Nov
                     <OPTION>12 Dec
                  </SELECT>
               </TD>
            </TR>
         </TABLE>
         <HR SIZE=1 NOSHADE>
         </FONT>
      </TD>
   </TR>
   <TR>
      <TD BGCOLOR=CCCCFF>
         <FONT FACE="ARIAL,HELVETICA,VERDANA">
         <CENTER><FONT SIZE=5>3. Deliver the poison...</FONT></CENTER>
         <P>
         <TABLE BORDER=0 CELLSPACING=1 CELLPADDING=1>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT>Shipping & Handling Option</TD>
               <TD>
                  <SELECT NAME=shippingOption>
                     <OPTION>US Mail (7 day delivery)
                     <OPTION SELECTED>UPS (2 day delivery)
                     <OPTION>Fedex (overnight delivery)
                  </SELECT>
               </TD>
            </TR>
            <TR>
               <TD COLSPAN=2 ALIGN=CENTER> </TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT><B>*Name</B></TD>
               <TD><INPUT TYPE=TEXT NAME=shippingAddressName SIZE=40></TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT><B>*Address Line 1</B></TD>
               <TD><INPUT TYPE=TEXT NAME=shippingAddressLine1 SIZE=40></TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT>Address Line 2</TD>
               <TD><INPUT TYPE=TEXT NAME=shippingAddressLine2 SIZE=40></TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT><B>*City</B></TD>
               <TD><INPUT TYPE=TEXT NAME=shippingAddressCity SIZE=40></TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT><B>*State</B> (or Province, or Region)</TD>
               <TD><INPUT TYPE=TEXT NAME=shippingAddressState SIZE=40></TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT><B>*Zip Code</B> (or Postal Code)</FONT></TD>
               <TD><INPUT TYPE=TEXT NAME=shippingAddressZip SIZE=40></TD>
            </TR>
            <TR VALIGN=TOP>
               <TD ALIGN=RIGHT><B>*Country</B></TD>
               <TD><INPUT TYPE=TEXT NAME=shippingAddressCountry SIZE=40 VALUE="USA"></TD>
            </TR>
         </TABLE>
         <HR SIZE=1 NOSHADE>
         </FONT>
      </TD>
   </TR>
   <TR>
      <TD BGCOLOR=000000>
         <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0>
            <TR>
               <TD ALIGN=LEFT>
                  <INPUT TYPE=reset VALUE="Reset and start over">
               </TD>
               <TD ALIGN=RIGHT>
                  <INPUT TYPE=submit NAME=submitButton VALUE="BUY the poison NOW!!!">
               </TD>
            </TR>
         </TABLE>
      </TD>
   </TR>
</TABLE>
<P>
</CENTER>
</FORM>

<SCRIPT LANGUAGE=JavaScript><!-- //

formSounds = new Music ();

function sonifyForms () {
   Music.noManualHandlers = true;
   document.productForm.submitButton.onclick = new Function ('return false');
   Music.sonifyFormsWithHandlers (
      'formSounds.play ("UI-Click7")',
      'formSounds.play ("UI-Click6")',
      'formSounds.play ("UI-Click2")',
      'formSounds.play ("UI-MouseOver1")',
      'formSounds.play ("UI-SimpleClick2")',
      'formSounds.stop (300)',
      'formSounds.stop (300)',
      'formSounds.play ("UI-SimpleClick2")',
      'formSounds.stop (300)',
      'formSounds.play ("Background-SimpleTyping")',
      'formSounds.play ("UI-CarriageReturn")',
      'formSounds.play ("UI-SimpleClick1")',
      'formSounds.play ("Fanfare-Mars")',
      'formSounds.play ("Fanfare-CorpSting1")'
   );
}

formSounds.stubEmbed ('stub.rmf');

// --></SCRIPT>

</BODY></HTML>

In the source code listing above, all the code needed for sonification of the form is highlighted in red. One of the first things you should notice, towards the top of the source code, is how the JavaScript file "music-object-x-wizards.js" that implements Music Object Extension - Wizards has been sourced into the page immediately following the sourcing in of the "music-object.js" file. It is important that Music Object extensions always be sourced after the core "music-object.js" file has first been sourced in, otherwise the extensions will not work.

Towards the bottom of the source code listing you will see the block of code (highlighted in red) that is responsible for actually sonifying the form. Three key things are taking place in this block of code...

  1. A Music Object instance is created so that it can be used to play Groovoids for the sonification of the form elements. The instance is created in the statement "formSounds = new Music ();".

  2. The sonifyForms function is defined, which gets called when the document has finished loading in (see the BODY tag's ONLOAD event handler towards the top of the example). Inside the implementation for this function there is only one statement - a call to the Music.sonifyFormsWithHandlers method, which is responsible for automatically adding the appropriate event handlers to all elements of the form.
     IMPORTANT : It's very important that the methods inside the "Music Object Extension - Wizards" module only be called once a document has finished loading in, as calling these methods while a document is still loading may, in some cases, cause unpredictable results.

    Now, remember the long list of parameters we discussed earlier? This is an example of those parameters in action. Wherever there is a form element event to be sonified you will see a string parameter containing a JavaScript statement that plays a Groovoid through the Music Object instance "formSounds". And wherever a form element event is not to be sonified you will see an empty string.

    In case you're finding it a little hard to map the parameter values in this method call to the parameters we discussed earlier, consider the following table...

    Parameter Name Parameter Value
    CheckboxCheckJavaScriptSTR 'formSounds.play ("UI-Click7")'
    CheckboxUncheckJavaScriptSTR 'formSounds.play ("UI-Click6")'
    RadioButtonClickJavaScriptSTR 'formSounds.play ("UI-Click2")'
    ListFocusJavaScriptSTR 'formSounds.play ("UI-MouseOver1")'
    ListChooseJavaScriptSTR 'formSounds.play ("UI-SimpleClick2")'
    ListBlurJavaScriptSTR 'formSounds.stop (300)'
    MultilistFocusJavaScriptSTR 'formSounds.stop (300)'
    MultilistChooseJavaScriptSTR 'formSounds.play ("UI-SimpleClick2")'
    MultilistBlurJavaScriptSTR 'formSounds.stop (300)'
    TextboxFocusJavaScriptSTR 'formSounds.play ("Background-SimpleTyping")'
    TextboxBlurJavaScriptSTR 'formSounds.play ("UI-CarriageReturn")'
    ButtonClickJavaScriptSTR 'formSounds.play ("UI-SimpleClick1")'
    FormResetJavaScriptSTR 'formSounds.play ("Fanfare-Mars")'
    FormSubmitJavaScriptSTR 'formSounds.play ("Fanfare-CorpSting1")'

  3. A Beatnik Player instance is embedded for the Music Object instance "formSounds" using the stubEmbed method.

Now, if you want to quickly go back and revisit the earlier step Adding the Submit and Reset Button Sounds, you may truly come to appreciate the power and convenience of Music Object extensions. Not only is the form sonification using this new approach easier, but it also is far less intrusive in the actual HTML source of your Web pages.

While the Music.sonifyFormsWithHandlers method is convenient, we didn't just stop there. In the next step of this tutorial you'll see how the Music.sonifyFormsWithGroovoids method makes things even easier still...